///\todo upgrade

//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#ifndef _BUOLA_IO_CFDSTREAM_H_
#define _BUOLA_IO_CFDSTREAM_H_

#include <buola/buola.h>
#include <buola/io/dstreamactivity.h>
#include <buola/io/cfdlistener.h>
#include <buola/io/cstream.h>

namespace buola { namespace io {

class CFDStream : public CStream, public CFDListener
{
public:
    CFDStream(int pFD,bool pCloseOnExit=true);
    virtual ~CFDStream();

public:
    bool IsOpen() const        {   return mFD!=-1;    }
    void Close();
    
    CResult Read(uint8_t *pData,size_t pLen) final;
    CResult Write(const uint8_t *pData,size_t pLen) final;
    off_t Seek(off_t pPos,ESeek pMode=ESeek::ABSOLUTE) final;

    void SetActivityDelegate(DStreamActivity *pDelegate,ENotify pNotify);

    int FD()     {   return mFD;     }

    void OnFDListener(int pFD,io::ENotify pEvent,void *pUserPtr) override;
    
private:
    int mFD;
    bool mCloseOnExit;
    
    delegate_ptr<DStreamActivity> mActivityDelegate;
};

typedef intrusive_ptr<CFDStream> PFDStream;

PFDStream open_disk_file(const std::string &pFilename,io::EMode pMode,io::EPermission pPerm);

/*namespace io*/ } /*namespace buola*/ }

#endif
